﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Antlr3.ST;
using System.IO;

namespace Web
{
    public class StringHash 
    {
        public string Name;
        public string Value;
    }

    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            StringBuilder rt = new StringBuilder();
            foreach (Type tclass in Assembly.Load("Web").GetTypes())
            {
                if (tclass.Namespace == "AshxData")
                {
                    rt.AppendLine("类:" + tclass.Name + " 命名空间：" + tclass.Namespace + "<br>");
                    rt.Append(PrintAuthorInfo(tclass));
                }
            }
            //Response.Write(rt.ToString());
            //Response.Write(Web.Core.Common.MD5("123", 32) + "<br>");
            //Response.Write(DateTime.Now.ToString());

            //Response.Write(PrintInfo());
            string strHostName = System.Net.Dns.GetHostName();
            string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
            //Response.Write(clientIPAddress);

            //string txt = "[{\"Name\":\"ghghgh\",\"Value\":\"11111111111\"},{\"Name\":\"123ghghgh\",\"Value\":\"13331111111111\"}]";
            //List<StringHash> lt = new List<StringHash>();
            //lt = Newtonsoft.Json.JavaScriptConvert.DeserializeObject<List<StringHash>>(txt);
            ////Response.Write(lt[1].Name);
            //lt = new List<StringHash>();
            //StringHash sh=new StringHash();
            //sh.Name = "user";
            //sh.Value="admin";
            //lt.Add(sh);
            //sh = new StringHash();
            //sh.Name = "pass";
            //sh.Value = "123";
            //lt.Add(sh);
            //Response.Write(Newtonsoft.Json.JavaScriptConvert.SerializeObject(lt));

            IDictionary<string, object> ret = new Dictionary<string, object>();
            ret["elem1"] = true;
            ret["elem2"] = "testthisiselem2";

            var nestedObj = new Dictionary<string, object>();
            nestedObj["nestedProp"] = 100;
            ret["elem3"] = nestedObj;

            StringTemplate template = new StringTemplate("$elem1$ or $elem2$ and value: $elem3.nestedProp$");
            template.Attributes = ret;

            StringBuilder sb = new StringBuilder();
            StringWriter writer = new StringWriter(sb);
            template.Write(new NoIndentWriter(writer));
            writer.Flush();

            Response.Write(sb.ToString());
        }

        private string PrintAuthorInfo(Type t)
        {
            StringBuilder rt = new StringBuilder();
            rt.AppendLine("类下所有方法及属性：<br>");

            MemberInfo[] myMembers = t.GetMembers(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            for (int i = 0; i < myMembers.Length; i++)
            {
                Object[] myAttributes = myMembers[i].GetCustomAttributes(true);
                if (myAttributes.Length > 0)
                {
                    rt.AppendLine("方法:" + myMembers[i].Name + "<br>");

                    AuthorAttr cls = (AuthorAttr)myAttributes[0];
                    rt.AppendLine("属性值：" + cls.Name + " - " + cls.Des + "<br>");
                }
            }
            return rt.ToString();
        }

        private string PrintInfo()
        {
            BindingFlags bfs = new BindingFlags();
            bfs = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly;
            MemberInfo[] myMembers;
            Object[] myAttributes;
            AuthorAttr cls;
            System.Text.StringBuilder json = new System.Text.StringBuilder();
            string forstr = "\"id\":{0},\"text\":\"{1}\",\"attributes\":{2}";
            int st = 0;

            foreach (Type tclass in Assembly.Load("Web").GetTypes())
            {
                if (tclass.Namespace == "AshxData")
                {
                    myAttributes = tclass.GetCustomAttributes(true);
                    if (myAttributes.Length > 0)
                    {
                        cls = (AuthorAttr)myAttributes[0];
                        if (cls.Check)
                        {
                            forstr = "{\"id\":" + st.ToString() + ",\"text\":\"" + cls.Name + "\"";

                            myMembers = tclass.GetMembers(bfs);
                            if (myMembers.Length > 0)
                            {
                                forstr += ",\"children\":[";
                            }
                            json.Append(forstr);
                            for (int i = 0; i < myMembers.Length; i++)
                            {
                                myAttributes = myMembers[i].GetCustomAttributes(true);
                                if (myAttributes.Length > 0)
                                {
                                    cls = (AuthorAttr)myAttributes[0];
                                    if (cls.Check)
                                    {
                                        //forstr = "{\"id\":"+st.ToString()+",\"text\":\""+cls.Name+" ("+cls.Des+")\",";
                                        forstr = "{\"id\":" + st.ToString() + ",\"text\":\"" + cls.Name + "\",";
                                        forstr += "\"attributes\":{\"func\":\"" + tclass.Name + "." + myMembers[i].Name + "\"}},";
                                        //json.Append("{" + String.Format(forstr,"{\"func\":\""+cls.Name+" ("+cls.Des+")\"}", tclass.Name + "." + myMembers[i].Name) + "},");
                                        json.Append(forstr);
                                    }
                                }
                            }
                            if (json.Length > 0)
                                json.Remove(json.Length - 1, 1);
                            if (myMembers.Length > 0) json.Append("]");
                        }
                    }
                }
            }
            if (json.Length > 0)
                json.Remove(json.Length - 1, 1);
            json.Append("]");
            json.Insert(0, "[");
            return json.ToString();
        }
    }
}